;;; init-magit.el --- Magit configuration -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:

(defun ctrlwei/magit-parent-project-status ()
  "Jump to parent project status buffer in submodule."
  (interactive)
  (let* ((directory default-directory)
         (parent-dir (expand-file-name (concat directory "../"))))
    (magit-status (magit-toplevel parent-dir))))

(with-eval-after-load 'magit
  (setq magit-auto-revert-mode t)
  (setq magit-section-visibility-indicator nil)
  (setq magit-log-margin '(t "%y/%m/%d|%W-%u %H:%M:%S %z" magit-log-margin-width t 18))
  ;; Kill all Magit buffers associated with the repo when quit repo's status buffer
  (define-key-with-arg magit-status-mode-map (kbd "q") magit-mode-bury-buffer 8)
  (setq magit-bury-buffer-function #'magit-restore-window-configuration)
  (define-key magit-status-mode-map (kbd "C-k") #'ctrlwei/magit-parent-project-status))

(with-eval-after-load 'transient
  (setq transient-display-buffer-action '(display-buffer-below-selected))
  (transient-bind-q-to-quit))

(global-set-key (kbd "C-x g") #'magit-status)
(global-set-key (kbd "s-g g") #'magit-status)
(global-set-key (kbd "s-g j") #'magit-dispatch)
(global-set-key (kbd "s-g k") #'magit-file-dispatch)
(global-set-key (kbd "s-g l") #'magit-log-buffer-file)
(global-set-key (kbd "s-g b") #'magit-blame)



(defun ctrlwei/vc-msg-sdk-format-datetime (seconds)
  (format-time-string "%Y/%m/%d|%W-%u %H:%M:%S" (seconds-to-time (string-to-number seconds))))
(advice-add #'vc-msg-sdk-format-datetime :override #'ctrlwei/vc-msg-sdk-format-datetime)

(setq vc-msg-git-show-commit-function 'magit-show-commit)

(global-set-key (kbd "C-x v n") #'vc-msg-show)

(provide 'init-magit)
;;; init-magit.el ends here
